package com.tech.koufu.tools;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import android.util.Log;

public class HttpUtils {
	public static String postRequest(String url,String cookie, Map<String, String>data){
		String result = null;
		BufferedReader reader = null;
		try {
			HttpClient client = new DefaultHttpClient();
			HttpPost request = new HttpPost(url);
			request.setHeader("Cookie", cookie);

			List<NameValuePair> params = new ArrayList<NameValuePair>();
			if(data != null && data.size() > 0){
				for(String key : data.keySet()){
					params.add(new BasicNameValuePair(key, data.get(key)));
				}
			}
			HttpParams hpparams = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(hpparams, 60000);
			request.setParams(hpparams);
			System.out.println("params=====>" + params);

			HttpEntity formEntity = new UrlEncodedFormEntity(params,"UTF-8");
			request.setEntity(formEntity);

			HttpResponse response = client.execute(request);
			reader = new BufferedReader(new InputStreamReader(response
					.getEntity().getContent(), "gbk"));

			StringBuffer strBuffer = new StringBuffer("");
			String line = null;
			while ((line = reader.readLine()) != null) {
				strBuffer.append(line);
			}
			result = strBuffer.toString();
			Log.v("StruBuffer=====>", "result is " + result);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  

		return result;
	}


	public static String getRequest(String url){
		String result = "";
		BufferedReader reader = null;
		HttpClient httpClient = new DefaultHttpClient();
		HttpGet get = new HttpGet(url);
		try {
//			get.setHeader("cookie", cookie);

			HttpParams httpParams = httpClient.getParams();
			HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
			HttpConnectionParams.setSoTimeout(httpParams, 5000);

			HttpResponse response = httpClient.execute(get);

			reader = new BufferedReader(new InputStreamReader(response
					.getEntity().getContent(), "gbk"));

			StringBuffer strBuffer = new StringBuffer("");
			String line = null;
			while ((line = reader.readLine()) != null) {
				strBuffer.append(line);
			}
			result = strBuffer.toString();
			Log.v("StruBuffer=====>", "result is " + result);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			get.abort();
		}
		return result;		
	}
	
	public static String getRequest(String url,Map<String, String>data){
		String result = "";
		BufferedReader reader = null;
		HttpClient httpClient = new DefaultHttpClient();
		HttpGet get = new HttpGet(url);

		try {
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			if(data != null && data.size() > 0){
				for(String key : data.keySet()){
					params.add(new BasicNameValuePair(key, data.get(key)));
				}
			}
//			HttpParams httpParams = httpClient.getParams();
			HttpParams httpParams = new BasicHttpParams();
			get.setParams(httpParams);
			HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
			HttpConnectionParams.setSoTimeout(httpParams, 5000);

			HttpResponse response = httpClient.execute(get);

			reader = new BufferedReader(new InputStreamReader(response
					.getEntity().getContent(), "gbk"));

			StringBuffer strBuffer = new StringBuffer("");
			String line = null;
			while ((line = reader.readLine()) != null) {
				strBuffer.append(line);
			}
			result = strBuffer.toString();
			Log.v("StruBuffer=====>", "result is " + result);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			get.abort();
		}
		return result;		
	}
}
